package com.zjs.db.sql.controller;

import com.util.result.Message;
import com.zjs.db.sql.service.SqlServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description SQL优化控制类
 * @Author zhu_jinsheng[zhu_js@suixingpay.com]
 * @Date 2020/2/16 22:35
 * @Version 1.0
 */
@Slf4j
@Controller
@RequestMapping("/SqlTest")
public class SqlController {
    @Autowired
    SqlServiceImpl sqlService;

    /**
     * 新增用户数据
     * @param num
     * @return
     */
    @ResponseBody
    @PostMapping("/createUserDate")
    public String createUserDate(Integer num){
        if (null == num || num <= 0) {
            return "请输入大于0的数字";
        }
        return sqlService.createUserDate(num);
    }

    /**
     * 新增用户订单数据
     * @param num
     * @return
     */
    @ResponseBody
    @PostMapping("/createOrderDateByUserId")
    public String createOrderDateByUserId(Integer num, Integer userId){
        log.info("订单用户：{}|订单数量：{}", userId, num);
        if (null == num || num <= 0) {
            return "请输入大于0的数字";
        }
        return sqlService.createOrderDateByUserId(num, userId);
    }

    /**
     * 通过订单号查询订单信息
     * @param orderNo 订单号
     * @return
     */
    @ResponseBody
    @PostMapping("/selectOrderByOrderNo")
    public Message selectOrderByOrderNo(String orderNo){
        log.info("入参：订单号：{}", orderNo);
        if (null == orderNo) {
            return Message.error("订单号不能为空");
        }
        try {
            return sqlService.selectOrderByOrderNo(orderNo);
        } catch (Exception e) {
            log.error("【查询{}订单数据异常】{}", orderNo, e);
            return Message.error();
        }
    }
}
